package com.kh.statistics.video.core.script;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.core.script
 * @Author: Hong.Kong
 * @CreateTime: 2024-06-14  16:28
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
public class DefualtParseExecResult implements ParseExecResult<String> {
    @Override
    public String parse(Map<String, List<String>> result, String parseStr) {
        List<String> normalList = result.get("normal");
        List<String> errorList = result.get("error");
        if (null != errorList && !errorList.isEmpty()) {
            log.warn("执行脚本失败失败信息为：{}", errorList);
            return null;
        }
        if (null == normalList || normalList.isEmpty()) {
            return null;
        }
        List<String> retList = normalList.stream().filter((item) -> item.startsWith(parseStr)).map((item) -> StringUtils.substringAfter(item, parseStr)).collect(Collectors.toList());
        if (retList.isEmpty()) {
            return null;
        }
        return retList.get(0);
    }
}
